﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;

namespace QuanLyHangXeKhach
{
    public partial class frmCapNhatGiaTuyen : UserControl
    {
        CapNhatGiaTuyen cngt = new CapNhatGiaTuyen();
        DieuKhien dk = new DieuKhien();

        public frmCapNhatGiaTuyen()
        {
            InitializeComponent();
        }

        private void frmCapNhatGiaTuyen_Load(object sender, EventArgs e)
        {
            lblthongbao.Text = "*Có " + cngt.tuyenchuacogia().ToString() + " tuyến đường chưa được cập nhật giá";
            autocompletegia();
            

            cobtuyen.Text = "";
            txtgia.Text = "";
            txtngay.Text = "";
            Loadfrm();
            this.cobtuyen.SelectedIndexChanged -= new System.EventHandler(this.cobtuyen_SelectedIndexChanged);
        }

        public void autocompletegia()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<giave> giave = cngt.laygia();
            foreach (var row in giave)
            {
                auto.Add(row.gia.ToString());
            }

            txtgia.AutoCompleteCustomSource = auto;
        }

        private void Loadfrm()
        {
            List<giatuyencapnhatgannhat> giatuyen = cngt.laygiagannhat();
            advTree1.Nodes.Clear();
            foreach (var row in giatuyen)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.tuyen, String.Format("{0:0,0 vnđ}", row.dongia), row.ngaycapnhatgannhat.ToString("dd/MM/yyyy")));
            }
        }

        private Node CreatenoidenRow(string tuyen, string gia, string ngay)
        {
            Node node = new Node(tuyen);
            node.Cells.Add(new Cell(gia));
            node.Cells.Add(new Cell(ngay));
            return node;            
        }


        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcngiatuyen;

            }
            else
            {
                btndong.Command = null;

            }
        }

        #endregion

        private void cobtuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cobtuyen.Text != "")
            {
                List<giatuyencapnhatgannhat> giatuyen = cngt.laygiatuyen(cobtuyen.SelectedValue.ToString());
                advTree1.Nodes.Clear();
                foreach (var row in giatuyen)
                {
                    advTree1.Nodes.Add(CreatenoidenRow(row.tuyen, String.Format("{0:0,0 vnđ}", row.dongia), row.ngaycapnhatgannhat.ToString("dd/MM/yyyy")));
                }
            }
            else Loadfrm();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if (btnthem.Text == "Cập nhật")
            {
                btnthem.Text = "Lưu";
                cobtuyen.DataSource = cngt.laytuyen();
                cobtuyen.DisplayMember = "ten";
                cobtuyen.ValueMember = "ma";

                txtngay.Text = System.DateTime.Now.ToString("dd/MM/yyyy");
                this.cobtuyen.SelectedIndexChanged += new System.EventHandler(this.cobtuyen_SelectedIndexChanged);
            }
            else
            {
                if (txtgia.Text == "")
                    MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    cngt.themgiatuyen(cobtuyen.SelectedValue.ToString(), System.DateTime.Now, txtgia.Text);
                    MessageBoxEx.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    autocompletegia();
                    lblthongbao.Text = "*Có " + cngt.tuyenchuacogia().ToString() + " tuyến đường chưa được cập nhật giá";
                    btnthem.Text = "Cập nhật";
                    cobtuyen.DataSource = null;
                    Loadfrm();
                    txtgia.Text = "";
                    txtngay.Text = "";
                    this.cobtuyen.SelectedIndexChanged += new System.EventHandler(this.cobtuyen_SelectedIndexChanged);
                }
            }
        }

        private void txtgia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
